// pages/my/stalls/stalls.js
Page({
  data: {
    active: 0,
    typeList: [{
      key: 2,
      value: "自家售卖"
    }, {
      key: 3,
      value: "出租招租"
    }, {
      key: 5,
      value: "二手商品"
    }, {
      key: 6,
      value: "旅游代买"
    }, {
      key: 7,
      value: "技能墙"
    }],
    type: "",
    list: [],
    page: 1,
    size: 10,
    isNoMore: true,
    isLoding: true
  },
  onLoad: function (options) {
    this.getList();
  },
  onShareAppMessage() {},
  reset() {
    this.setData({
      list: [],
      page: 1,
      isNoMore: true,
      isLoding: true
    });
    this.getList();
  },
  tab(e) {
    this.setData({
      active: e.detail.index,
      list: [],
      page: 1,
      isNoMore: true,
      isLoding: true
    });
    this.getList();
  },
  typeSel(e) {
    this.setData({
      type: e.currentTarget.dataset.type,
      list: [],
      page: 1,
      isNoMore: true,
      isLoding: true
    });
    this.getList();
  },
  getList() {
    let params = {
      page: this.data.page,
      size: this.data.size,
      saleStatus: this.data.active == 0 ? 1 : 0,
      type: this.data.type,
    }
    getApp().globalData.http.myStall(params).then(res => {
      let data_tmp = [...this.data.list, ...res.data];
      this.setData({
        list: data_tmp,
        isLoding: true
      });
      if (this.data.list.length == res.count) {
        this.setData({
          isNoMore: false
        });
      }
    });
  },
  onReachBottom() {
    if (this.data.isNoMore) {
      this.setData({
        page: this.data.page / 1 + 1,
        isLoding: false
      });
      this.getList();
    }
  },
  takedown(e) {
    getApp().globalData.vant.dialog.confirm({
        title: "确定下架吗?",
        message: "下架后服务将无法被邻居们看到",
        confirmButtonText: "下架",
        confirmButtonColor: "#33B868"
      })
      .then(() => {
        getApp().globalData.http.stallDown({
          id: e.currentTarget.dataset.id
        }).then(res => {
          getApp().globalData.vant.toast("下架成功");
          this.reset();
        });
      })
      .catch(() => {});
  },
  edit(e) {
    let type = e.currentTarget.dataset.type;
    let url = "";
    if (type == 2) {
      url = "/pages/add/sell/sell?edit=1&id=";
    } else if (type == 3) {
      url = "/pages/add/rent/rent?edit=1&id=";
    } else if (type == 5) {
      url = "/pages/add/oldgood/oldgood?edit=1&id=";
    } else if (type == 6) {
      url = "/pages/add/travelbuy/travelbuy?edit=1&id=";
    } else if (type == 7) {
      url = "/pages/add/skills/skills?edit=1&id=";
    }
    wx.navigateTo({
      url: url + e.currentTarget.dataset.id,
    })
  },
  del(e) {
    getApp().globalData.vant.dialog.confirm({
        title: "确定删除吗?",
        message: "删除后数据将无法恢复",
        confirmButtonText: "删除",
        confirmButtonColor: "#33B868"
      })
      .then(() => {
        getApp().globalData.http.stallRemove({
          id: e.currentTarget.dataset.id
        }).then(res => {
          getApp().globalData.vant.toast("删除成功");
          this.reset();
        });
      })
      .catch(() => {});
  }
})